package test.enba.mq.producer.controller.rocketmq;

import com.enba.boot.mq.MqHelper;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/rocketmq")
public class MessageController {

  /*
   *
   * 点对点==》 所有消费者放一个消费者组
   *
   * 发布订阅==》 不同消费者放不同消费者组
   * */

  @Autowired private RocketMQTemplate rocketMQTemplate;

  @Autowired private MqHelper mqHelper;

  @PostMapping("/queue/send-message")
  public String sendMessage(@RequestParam String message) {
    rocketMQTemplate.convertAndSend("test-queue", message);
    return "Message sent: " + message;
  }

  @PostMapping("/topic/send-message")
  public String sendPubSubMessage(@RequestParam String message) {
    rocketMQTemplate.convertAndSend("pubsub-topic", message);
    return "Pub/Sub Message sent: " + message;
  }

  @PostMapping("/queue2/send-message")
  public String sendMessage2(@RequestParam String message) {
    return mqHelper.sendMessageByQueue("test-queue", message, () -> "Message sent: " + message);
  }

  @PostMapping("/topic2/send-message")
  public String sendPubSubMessage2(@RequestParam String message) {
    return mqHelper.sendMessageByTopic(
        "pubsub-topic", message, () -> "Pub/Sub Message sent: " + message);
  }
}
